package com.etc.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.etc.entity.Admin;
import com.etc.entity.Article;
import com.etc.service.AdminService;
import com.etc.service.ArticleService;
import com.etc.utils.EnDecryptUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
public class AdminController_lc {

	private final static Integer PAGESIZE = 15;
	@Autowired
	private ArticleService articleService;
	@Autowired
	private AdminService adminService;
	
	@RequestMapping("admin-logout")
	public String adminLogout(HttpSession session){
		session.removeAttribute("admin");
		return "admin-login";
	}
	
	@RequestMapping("admin-checkLogin")
	public String showAdminLogin(Admin admin,HttpServletRequest request){
		Admin checkAdmin = adminService.checkAdminByAname(admin.getAname());
		if (checkAdmin!=null) {
			if (EnDecryptUtil.SHA1(admin.getApwd()).equals(checkAdmin.getApwd())) {
				checkAdmin.setApwd(null);
				request.getSession().setAttribute("admin", checkAdmin);
				return "admin-main";
			}
		}
		request.setAttribute("errmsg", "您的用户名或者密码错误");
		return "admin-login";
	}
	
	@RequestMapping("deleteArtilceByaAid/{aid}")
	public String deleteArtilceByaAid(@PathVariable("aid")Integer aid){
		
		articleService.deleteArticleByAid(aid);
		
		return "redirect:/admin-articleinfo";			
		
	}
	
	@RequestMapping("admin-upadteAstatus")
	@ResponseBody
	public String upadteAstatus(@RequestParam("aid")Integer aid,@RequestParam("astatus")Boolean astatus){
		Article article = articleService.selectArticleByAid(aid);
		article.setAstatus(astatus);
		int i = articleService.updateArticle(article);
		if (i>0) {
			return "success";
		}else{
			return "error";
		}
		
	}
	
	@RequestMapping("admin-view")
	public String showAdminView(Map<String, Object> map,@RequestParam("aid")Integer aid){
		Article article = articleService.selectArticleByAid(aid);
		map.put("article", article);
		
		return "admin-view";
	}

	@RequestMapping("admin-articleinfo")
	public String showAdminArticleInfo(Map<String, Object> map,
			@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
			@RequestParam(value = "astatus", defaultValue = "all") String astatus,
			@RequestParam(value = "atitle", defaultValue = "") String atitle) {
		// 对文章进行分页
		PageHelper.startPage(pageNum, PAGESIZE);

		List<Article> articleList = new ArrayList<Article>();

		if ("".equals(atitle) == false) {// 判断模糊查询的文章
			articleList = articleService.findArticleInfoByLikeATitle(atitle);
		} else if ("all".equals(astatus)) {
			articleList = articleService.findAllArticleInfo();
		} else if ("yes".equals(astatus)) {
			articleList = articleService.findAStatusIsYesArticleInfo();
		} else if ("all".equals(astatus)) {
			articleList = articleService.findAllArticleInfo();
		} else if ("no".equals(astatus)) {
			articleList = articleService.findAStatusIsNoArticleInfo();
		} else {
			articleList = articleService.findAllArticleInfo();
		}

		PageInfo<Article> pageInfo = new PageInfo<Article>(articleList);

		map.put("articleList", articleList);
		map.put("page", pageInfo);
		// 将审核标签的状态信息返回给前端
		map.put("astatus", astatus);
		map.put("atitle", atitle);
		return "admin-articleinfo";
	}
}
